#define _CRT_SECURE_NO_WARNINGS 1

#include"stack.h"

void StackInit(Stack* ps)
{
	ps->_a = (STDataType *)malloc(STACKDATATOP * sizeof(STDataType));
	ps->_capacity = STACKDATATOP;
	ps->_top = 0;
}


void StackPush(Stack* ps, STDataType data)
{
	if (ps->_top == ps->_capacity)
	{
		ps->_capacity *= 2;
		STDataType* cur = NULL;
		cur = (STDataType*)realloc(ps->_a, ps->_capacity * sizeof(STDataType));
		if (cur == NULL)
		{
			perror("realloc");
			return;
		}
		ps->_a = cur;
	}
	ps->_a[ps->_top] = data;
	ps->_top++;
}

void StackPop(Stack* ps)
{
	if (ps->_top == 0)
	{
		return;
	}
	ps->_top--;
}

STDataType StackTop(Stack* ps)
{
	if (ps->_top == 0)
	{
		return (STDataType)0;
	}

	return ps->_a[ps->_top - 1];
}

int StackSize(Stack* ps)
{
	return ps->_top;
}

int StackEmpty(Stack* ps)
{
	if (ps->_top == 0)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

void StackDestroy(Stack* ps)
{
	assert(ps);
	assert(ps->_a);
	free(ps->_a);
	ps->_a = NULL;
	ps->_top = 0;
	ps->_capacity = 0;
}

